﻿
var ns, toast, actionsheet;
var app = new Vue({
    el: '#app',
    data: {
        RoomID: 0,
        RoomIdList: [],
        allroomList: [],
        roomList: [],
        chargeList: [],
        form: {
            id: 0,
            roomId: 0,
            fullName: '阳光雅筑-住宅-1栋',
            chargeId: 0,
            chargeName: '',
            unitPrice: '',
            useCount: '',
            totalCost: '',
            chargeName: '',
            startTime: '',
            endTime: '',
            remark: '',
            feeDate: ''
        },
        IsMaskOpen: false,
        IsChooseRoom: false,
        IsChooseCharge: false,
        submiting: false,
        radio: '1',
        showPopover: false,
        chosenTypeName: '',
        chosenTypeId: 0,
        totalCostPlaceHolder: '请输入'
    },
    computed: {
        formChargeId() {
            return this.form.chargeId;
        },
        formRoomId() {
            return this.form.roomId;
        },
        formUnitPrice() {
            return this.form.unitPrice;
        },
        formUseCount() {
            return this.form.useCount;
        },
        formStartTime() {
            return this.form.startTime;
        },
        formEndTime() {
            return this.form.endTime;
        }
    },
    watch: {
        formChargeId(val) {
            this.calculateUnitPrice();
        },
        formRoomId(val) {
            this.calculateUnitPrice();
        },
        formUnitPrice(val) {
            this.calculateCost();
        },
        formUseCount(val) {
            this.calculateCost();
        },
        formStartTime(val) {
            this.calculateCost();
        },
        formEndTime(val) {
            this.calculateCost();
        }
    },
    methods: {
        get_data: function () {
            var that = this;
            that.allroomList = [];
            that.roomList = [];
            ns.post({
                action: 'getroomfeeaddparams',
                RoomID: that.RoomID,
            }, function (succeed, data, err) {
                if (succeed) {
                    that.form.startTime = data.startTime;
                    that.form.endTime = data.endTime;
                    that.form.feeDate = data.feeDate;
                    that.allroomList = data.roomList;
                    data.roomList.forEach(function (item) {
                        if (that.RoomIdList.indexOf(item.id) == -1) {
                            return true;
                        }
                        that.roomList.push(item);
                        if (item.id == that.RoomID) {
                            item.isSelected = true;
                            that.form.roomId = item.id;
                            that.form.fullName = item.name;
                            return false;
                        }
                    })
                    that.chargeList = data.chargeList;
                    if (data.chargeList.length > 0) {
                        data.chargeList[0].isSelected = true;
                        that.form.chargeId = data.chargeList[0].id;
                        that.form.chargeName = data.chargeList[0].name;
                        that.chosenTypeName = data.chargeList[0].chargeType;
                        that.chosenTypeId = data.chargeList[0].typeId;
                        if (that.chosenTypeId > 0 && this.radio == 2) {
                            that.totalCostPlaceHolder = '根据公式计算';
                        } else {
                            that.totalCostPlaceHolder = '请输入';
                        }
                    }
                }
            });
        },
        calculateUnitPrice: function () {
            var that = this;
            if (!that.form.chargeId || !that.form.roomId) {
                that.form.totalCost = 0
                return;
            }
            ns.post({
                action: 'calculateunitpricebyrule',
                chargeId: that.form.chargeId,
                roomId: that.form.roomId,
            }, function (succeed, data, err) {
                if (succeed) {
                    that.form.unitPrice = data.unitPrice;
                    that.form.useCount = data.useCount;
                }
            });
        },
        calculateCost: function () {
            var that = this;
            if (!that.form.chargeId || !that.form.roomId) {
                that.form.totalCost = 0
                return;
            }
            if (that.chosenTypeId <= 0) {
                return;
            }
            ns.post({
                action: 'calculatetotalcostbyrule',
                chargeId: that.form.chargeId,
                roomId: that.form.roomId,
                startTime: that.form.startTime,
                endTime: that.form.endTime,
                useCount: that.form.useCount,
                unitPrice: that.form.unitPrice
            }, function (succeed, data, err) {
                if (succeed) {
                    that.form.totalCost = data.cost;
                }
            });
        },
        onShowPopover: function () {
            this.showPopover = !this.showPopover;
        },
        do_choose_room: function () {
            var that = this;
            that.IsMaskOpen = true;
            that.IsChooseRoom = true;
            that.IsChooseCharge = false;
        },
        do_choose_charge: function () {
            var that = this;
            that.IsMaskOpen = true;
            that.IsChooseRoom = false;
            that.IsChooseCharge = true;
        },
        doSelectRoom: function (item) {
            var that = this;
            that.roomList.forEach(function (item) {
                item.isSelected = false;
            })
            item.isSelected = true;
            that.form.roomId = item.id;
            that.form.fullName = item.name;
            that.doCloseMask();
        },
        doSelectCharge: function (item) {
            var that = this;
            that.chargeList.forEach(function (item) {
                item.isSelected = false;
            })
            item.isSelected = true;
            that.form.chargeId = item.id;
            that.form.chargeName = item.name;
            that.chosenTypeName = item.chargeType;
            that.chosenTypeId = item.typeId;
            if (that.chosenTypeId > 0 && this.radio == 2) {
                that.totalCostPlaceHolder = '根据公式计算';
            } else {
                that.totalCostPlaceHolder = '请输入';
            }
            that.doCloseMask();
        },
        do_select_time: function (type) {
            var that = this;
            var currentDate = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentDate,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentDate = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentDate;
                    } else {
                        that.form.endTime = currentDate;
                    }
                }
            });
        },
        doCloseMask: function () {
            var that = this;
            that.IsMaskOpen = false;
            that.IsChooseRoom = false;
            that.IsChooseCharge = false;
        },
        do_save: function (item) {
            var that = this;
            if (!that.form.roomId) {
                api.toast({
                    msg: '请选择资源位置',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.chargeId) {
                api.toast({
                    msg: '请选择收费项目',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.totalCost) {
                api.toast({
                    msg: '请输入应收金额',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            that.submiting = true;
            ns.post({
                action: 'savetemproomfee',
                feeStartTime: that.form.startTime,
                feeEndTime: that.form.endTime,
                totalCost: that.form.totalCost,
                useCount: that.form.useCount,
                chargeId: that.form.chargeId,
                roomId: that.form.roomId,
                unitPrice: that.form.unitPrice,
                remark: that.form.remark,
                feeDate: that.form.feeDate
            }, function (succeed, data, err) {
                that.submiting = false;
                if (succeed) {
                    api.toast({
                        msg: '保存成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.id = data.feeId;
                    api.sendEvent({
                        name: 'saveroomfeetempaddfield',
                        extra: {
                            dataField: JSON.stringify(that.form),
                        }
                    });
                    setTimeout(() => {
                        api.closeWin();
                    }, 1000);
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.RoomID = api.pageParam.id || '';
    app.RoomIdList = eval('(' + api.pageParam.roomIds + ')');
    app.get_data();
};
